<?xml version="1.0" encoding="UTF-8" ?>

<!-- One XSLT that use HTML + CSS to display the list of players showing, for each player, 
the name, the nick.-name, the club, the nationality, the number of scored goals, the 
number of games played, the number of times he played at initial time (“titular”) and some 
additional information if had consider important in your language. The XSLT should had 3 
parameters: as in the first XSLT two parameters will be used to control the sort order 
and the third parameter will filter the displayed rows -->

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

	<xsl:output method="xml" indent="yes"/>
	
	<xsl:template match="/">
	<xsl:processing-instruction name="xml-stylesheet">type="text/xsl" href="xslt/players_page.xslt"</xsl:processing-instruction>
	
	<players>
		<xsl:for-each select="/league/players/player">
			<xsl:variable name="playerId" select="@id"/>
		<player>
	
		<id><xsl:value-of select="@id" /></id>
		<name><xsl:value-of select="playerName" /></name>
		<nationality><xsl:value-of select="nationality" /></nationality>
		<height><xsl:value-of select="height" /></height>
		<weight><xsl:value-of select="weight" /></weight>
		<nickname><xsl:value-of select="nickname" /></nickname>
		<bday><xsl:value-of select="bday" /></bday>
		<position><xsl:value-of select="@position" /></position>
		
		
		<xsl:variable name="lineup" select="count(//game_team[@teamId = /league/teams/team[./squad/squad_player[@playerId = $playerId]]/@id]/initial[contains(@playersId, $playerId)] )"/>
		<lineup>
			<xsl:value-of select="$lineup" />
		</lineup>
		
		<games>
			<xsl:value-of select="$lineup+count(/league/fixtures/game/substitutions/substitution/in[@playerId = $playerId])" />
		</games>

		<xsl:apply-templates select="/league/teams/team[./squad/squad_player[@playerId = $playerId]]">
			<xsl:with-param name="player" select="@id"></xsl:with-param>
		</xsl:apply-templates>
		
		
		<xsl:variable name="yellow" select="count(/league/fixtures/game/discipline/card[@playerId = $playerId]/cardtype[@color= 'yellow'])"/>
		<xsl:variable name="red" select="count(/league/fixtures/game/discipline/card[@playerId = $playerId]/cardtype[@color= 'red'])"/>
		<cards>
			<xsl:value-of select="$yellow+$red" />			
		</cards>
		

		<goalsScored>
			<xsl:value-of select="count(/league/fixtures/game/score/goals/goal[@playerId = $playerId])" />
		</goalsScored>
	</player>
	</xsl:for-each>
	
	</players>
	</xsl:template>

	<xsl:template match="team">
		<xsl:param name="player" />
		<team>
			<xsl:attribute name="teamId" select="@id" />
			<xsl:value-of select="teamName" />
					
		</team>		
		<playerNumber><xsl:value-of select="./squad/squad_player[@playerId = $player]/number" /></playerNumber>
	</xsl:template>


</xsl:stylesheet>

